package com.mhkj.revise.monthPlan.controller;

import com.mhkj.base.fruitType.entity.FruitType;
import com.mhkj.revise.monthPlan.entity.Follow;
import com.mhkj.revise.monthPlan.service.PlanMonthAddService;
import com.mhkj.revise.monthPlan.service.PlanMonthService;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mhkj.workspace.notify.entity.Notify;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.util.UserHelper;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/followm")
public class FollowController extends BaseController<Follow> {
    @Resource
    PlanMonthAddService planMonthAddService;
    @Resource
    private JsonHandler jsonHandler;
    @Resource
    private OrgService orgService;
    @Resource
    private PlanMonthService planMonthService;
    //工作台本月关注计划，详情界面
    @RequestMapping("/listByPageinfo")
    @ResponseBody
    public void listByPageinfo(HttpServletResponse response,  Follow follow,String nodeName, String yearMonth,String state,int pageIndex, int pageSize) throws ParseException {
        String employeeId = UserHelper.getCurrentUserId();
        Employee naem = orgService.employeeInfo(employeeId);

        if(state==null) {
            state="";
        }
        String firstDay="";
        String lastDay="";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            if(yearMonth!=null) {

                Date formattime = new SimpleDateFormat("yyyy-MM").parse(yearMonth);

                firstDay=format.format(formattime.getTime());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(formattime);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
                lastDay=format.format(calendar.getTime());
            }
        Pagination<Follow> pagination  = planMonthAddService.listByPageinfo(follow,nodeName, yearMonth,state,firstDay,lastDay,pageIndex, pageSize);
        List<Follow> followList = pagination.getData();
        List<Follow> follows = planMonthAddService.findAll();
        for (Follow follow1 : followList) {
            String id = follow1.getPlanNode().getPostId();
            List<Employee> postInfo = orgService.getEmployeeByPost(id);
            if (postInfo.size() > 0) {
                //System.out.println("252222222"+postInfo.get(0).getName());
                follow1.setName(postInfo.get(0).getName());
            }
        }

        jsonHandler.sendJsonString(response, true, "获取数据成功", pagination,
                new String[] { "id","planNode.plan.name","planNode.nodeName","planNode.nodeProperty.value","planNode.completeStandard","planNode.state","planNode.rate","planNode.beginDate","planNode.endDate","planNode.remark","planNode.business.typeName","planNode.plan.planType.value","name","planNode.delayDate","planNode.riskState"},
                new String[] { "id","planName","nodeName","nodePropertyName","completeStandard","state","rate","beginDate","endDate","remark","businessName","planTypeName","postName","delayDate","riskState"});

    }

    @RequestMapping(value="/del", method= RequestMethod.POST)
    @ResponseBody
    public ResponseMessage del(Follow y) {
        String message = planMonthAddService.del(y);
        return ResponseMessage.createMessage("ok".equals(message), message);
    }
}
